#include "frame_processor.h"
#include <chrono>
#include <thread>
#include <random>

// 采集处理器实现
CaptureProcessor::CaptureProcessor(double min_delay, double max_delay)
    : rng_(std::random_device{}()), dist_(min_delay, max_delay) {}

std::shared_ptr<VideoFrame> CaptureProcessor::process(std::shared_ptr<VideoFrame> frame) {
    // 生成随机延迟并模拟处理时间
    double delay_ms = dist_(rng_);
    std::this_thread::sleep_for(std::chrono::microseconds(static_cast<int>(delay_ms * 1000)));
    
    // 设置采集时间戳
    frame->setCaptureTime(std::chrono::system_clock::now());
    return frame;
}

// 编码处理器实现
EncodeProcessor::EncodeProcessor(double min_delay, double max_delay)
    : rng_(std::random_device{}()), dist_(min_delay, max_delay) {}

std::shared_ptr<VideoFrame> EncodeProcessor::process(std::shared_ptr<VideoFrame> frame) {
    // 生成随机延迟并模拟处理时间
    double delay_ms = dist_(rng_);
    std::this_thread::sleep_for(std::chrono::microseconds(static_cast<int>(delay_ms * 1000)));
    
    // 设置编码时间戳
    frame->setEncodeTime(std::chrono::system_clock::now());
    return frame;
}

// 发送处理器实现
SendProcessor::SendProcessor(double min_delay, double max_delay)
    : rng_(std::random_device{}()), dist_(min_delay, max_delay) {}

std::shared_ptr<VideoFrame> SendProcessor::process(std::shared_ptr<VideoFrame> frame) {
    // 生成随机延迟并模拟处理时间
    double delay_ms = dist_(rng_);
    std::this_thread::sleep_for(std::chrono::microseconds(static_cast<int>(delay_ms * 1000)));
    
    // 设置发送时间戳
    frame->setSendTime(std::chrono::system_clock::now());
    return frame;
}

// 接收处理器实现
ReceiveProcessor::ReceiveProcessor(double min_delay, double max_delay)
    : rng_(std::random_device{}()), dist_(min_delay, max_delay) {}

std::shared_ptr<VideoFrame> ReceiveProcessor::process(std::shared_ptr<VideoFrame> frame) {
    // 生成随机延迟并模拟处理时间
    double delay_ms = dist_(rng_);
    std::this_thread::sleep_for(std::chrono::microseconds(static_cast<int>(delay_ms * 1000)));
    
    // 设置接收时间戳
    frame->setReceiveTime(std::chrono::system_clock::now());
    return frame;
}

// 解码处理器实现
DecodeProcessor::DecodeProcessor(double min_delay, double max_delay)
    : rng_(std::random_device{}()), dist_(min_delay, max_delay) {}

std::shared_ptr<VideoFrame> DecodeProcessor::process(std::shared_ptr<VideoFrame> frame) {
    // 生成随机延迟并模拟处理时间
    double delay_ms = dist_(rng_);
    std::this_thread::sleep_for(std::chrono::microseconds(static_cast<int>(delay_ms * 1000)));
    
    // 设置解码时间戳
    frame->setDecodeTime(std::chrono::system_clock::now());
    return frame;
}

// 渲染处理器实现
RenderProcessor::RenderProcessor(double min_delay, double max_delay)
    : rng_(std::random_device{}()), dist_(min_delay, max_delay) {}

std::shared_ptr<VideoFrame> RenderProcessor::process(std::shared_ptr<VideoFrame> frame) {
    // 生成随机延迟并模拟处理时间
    double delay_ms = dist_(rng_);
    std::this_thread::sleep_for(std::chrono::microseconds(static_cast<int>(delay_ms * 1000)));
    
    // 设置渲染时间戳
    frame->setRenderTime(std::chrono::system_clock::now());
    return frame;
}

// 处理管道实现
ProcessingPipeline::ProcessingPipeline() {}

void ProcessingPipeline::addProcessor(std::unique_ptr<FrameProcessor> processor) {
    processors_.push_back(std::move(processor));
}

std::shared_ptr<VideoFrame> ProcessingPipeline::processFrame(std::shared_ptr<VideoFrame> frame) {
    std::shared_ptr<VideoFrame> current_frame = frame;
    for (const auto& processor : processors_) {
        current_frame = processor->process(current_frame);
        if (!current_frame) break;
    }
    return current_frame;
}
